package com.flycent.fanbao.ui;


import java.util.List;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;

import com.flycent.fanbao.R;
import com.flycent.fanbao.bean.Common;
import com.flycent.fanbao.service.LocationService;

public class LocationManageActivity extends BackableTitleBarActivity{
	private TextView text1,text2,text3,text4;  
	private ProgressDialog dialog;  
	private String IP = "http://maps.google.com/maps/api/geocode/json?language=zh-CN&sensor=false&latlng=";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location);
		getTitleBar().setTitle("LocationManageActivity");

		text1 = (TextView)findViewById(R.id.jingdu);
		text2 = (TextView)findViewById(R.id.weidu);
		text3 = (TextView)findViewById(R.id.address);
		text4 = (TextView)findViewById(R.id.address1);

		// 注册广播  
		IntentFilter filter = new IntentFilter();  
		filter.addAction(Common.LOCATION_ACTION);  
		this.registerReceiver(new LocationBroadcastReceiver(), filter);  

		// 启动服务  
		Intent intent = new Intent();  
		intent.setClass(this, LocationService.class);  
		startService(intent);  

		// 等待提示  
		dialog = new ProgressDialog(this);  
		dialog.setMessage("正在定位...");  
		dialog.setCancelable(true);  
		dialog.show();  
		
	}  

	private class LocationBroadcastReceiver extends BroadcastReceiver {  

		@Override  
		public void onReceive(Context context, Intent intent) {  
			if (!intent.getAction().equals(Common.LOCATION_ACTION)) 
				return;  
			String locationInfo = intent.getStringExtra(Common.LOCATION);  
			System.out.println("locationInfo===="+locationInfo);

			String[] arr = locationInfo.split(" ");
			String[] locations = arr[1].split(",");
			String lng = locations[1];
			String lat = locations[0];
			System.out.println(lng);
			System.out.println(lat);


			text1.setText(lat);  
			text2.setText(lng);
			double aa = Double.parseDouble(lng);
			double bb = Double.parseDouble(lat);
			
			String address = getAddress(bb, aa);

			text3.setText(address);	
			dialog.dismiss();

			LocationManageActivity.this.unregisterReceiver(this);// 不需要时注销  
		}  
	}  

	public String getAddress(double latitude, double longitude){
		String strAddress = "";
		Geocoder geocoder = new Geocoder(this);
		List places = null;
		try{
			places = geocoder.getFromLocation(latitude, longitude, 5); 
			text4.setText(""+places);
			System.out.println("places===="+places);
		}catch(Exception e)        {
			e.printStackTrace();
		}
		if(places != null && places.size() > 0){
			System.out.println(("Address"+places.get(0)));
			strAddress = ((Address)places.get(0)).getAddressLine(1);
			
			System.out.println("strAddress==="+((Address)places.get(0)).getLocality()   );
			                 
		}
		return strAddress;       
	}


}


